package com.wnxy.list;

import javax.swing.plaf.basic.BasicTreeUI;
import java.awt.*;

// 定义节点，需要定义几个元素？
class Node{
    // 元素数据
    public Object item;
    // pre指向上一个节点
    public Node pre;
    // next指向下一个节点
    public Node next;

    public Node(Object item) {
        this.item = item;
    }
    @Override
    public String toString() {
        return this.item.toString();
    }
}
public class App5 {

    public static void main(String[] args) {
        // 构造一个双向链表

        Node jacky = new Node("jacky");
        Node lili = new Node("lili");
        Node jet = new Node("Jet");

        // jacky --> lili --> jet
        jacky.next = lili;
        lili.next = jet;

        // jet --> lili --> jacky
        jet.pre = lili;
        lili.pre = jacky;

        // 链表的头节点
        Node first = jacky;
        // 链表的尾节点
        Node last = jet;

        // 1、遍历链表：从头到尾
        System.out.println("1、遍历链表：从头到尾");
        while (true) {
            if (first == null) {
                break;
            }
            System.out.println(first.toString());
            first = first.next;
        }

        System.out.println("2、遍历链表：从尾到头");
        while (true) {
            if (last == null) {
                break;
            }
            System.out.println(last);
            last = last.pre;
        }

        System.out.println("3. lili与jet之间，插入一个节点");
        Node tom = new Node("tom");
        tom.next = jet;
        tom.pre = lili;

        lili.next = tom;
        jet.pre = tom;

        System.out.println("4. 重新遍历：从头到尾");
        first = jacky;
        while (true) {
            if (first == null) {
                break;
            }
            System.out.println(first);
            first = first.next;
        }

    }
}
